package com.ruoyi.iot.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.iot.domain.IotRulesAction;
import com.ruoyi.iot.service.IIotRulesActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 规则执行动作Controller
 *
 * @author ruoyi
 * @date 2025-03-28
 */
@RestController
@RequestMapping("/iot/action")
public class IotRulesActionController extends BaseController {
    @Autowired
    private IIotRulesActionService iotRulesActionService;

    /**
     * 查询规则执行动作列表
     */
    @GetMapping("/list")
    public TableDataInfo list(IotRulesAction iotRulesAction) {
        startPage();
        List<IotRulesAction> list = iotRulesActionService.selectIotRulesActionList(iotRulesAction);
        return getDataTable(list);
    }

    /**
     * 导出规则执行动作列表
     */
    @Log(title = "规则执行动作", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotRulesAction iotRulesAction) {
        List<IotRulesAction> list = iotRulesActionService.selectIotRulesActionList(iotRulesAction);
        ExcelUtil<IotRulesAction> util = new ExcelUtil<IotRulesAction>(IotRulesAction.class);
        util.exportExcel(response, list, "规则执行动作数据");
    }

    /**
     * 获取规则执行动作详细信息
     */
    @GetMapping(value = "/{actionId}")
    public AjaxResult getInfo(@PathVariable("actionId") Long actionId) {
        return success(iotRulesActionService.selectIotRulesActionByActionId(actionId));
    }

    /**
     * 新增规则执行动作
     */
    @Log(title = "规则执行动作", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IotRulesAction iotRulesAction) {
        return toAjax(iotRulesActionService.insertIotRulesAction(iotRulesAction));
    }

    /**
     * 修改规则执行动作
     */
    @Log(title = "规则执行动作", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotRulesAction iotRulesAction) {
        return toAjax(iotRulesActionService.updateIotRulesAction(iotRulesAction));
    }

    /**
     * 删除规则执行动作
     */
    @Log(title = "规则执行动作", businessType = BusinessType.DELETE)
    @DeleteMapping("/{actionIds}")
    public AjaxResult remove(@PathVariable Long[] actionIds) {
        return toAjax(iotRulesActionService.deleteIotRulesActionByActionIds(actionIds));
    }
}
